package com.mogist.hqc.module.main.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v7.widget.CardView;
import android.view.ViewGroup;

import com.mogist.hqc.entitys.response.MainItemVo;
import com.mogist.hqc.module.main.ui.CardFragment;

import java.util.ArrayList;
import java.util.List;

public class CardFragmentPagerAdapter extends FragmentStatePagerAdapter implements CardAdapter
{

	private List<CardFragment> mFragments = new ArrayList<>();
	private float mBaseElevation;

	public CardFragmentPagerAdapter(FragmentManager fm, float baseElevation, List<MainItemVo> list)
	{
		super(fm);
		mFragments = new ArrayList<>();
		mBaseElevation = baseElevation;

		for(int i = 0; i < list.size(); i++)
		{
			CardFragment fragment = new CardFragment();
			Bundle bundle = new Bundle();
			bundle.putInt("pos", i);
			fragment.setArguments(bundle);
			addCardFragment(fragment);
		}
	}

	@Override public float getBaseElevation()
	{
		return mBaseElevation;
	}

	@Override public CardView getCardViewAt(int position)
	{
		if(mFragments.size()>position)
		{
			return mFragments.get(position).getCardView();
		}
		else
		{
			return null;
		}
	}

	@Override public int getCount()
	{
		return mFragments.size();
	}

	@Override public Fragment getItem(int position)
	{
		if(mFragments.size()>position)
		{
			return mFragments.get(position);
		}
		else
		{
			return null;
		}
	}

	@Override public Object instantiateItem(ViewGroup container, int position)
	{
		Object fragment = super.instantiateItem(container, position);
		mFragments.set(position, (CardFragment) fragment);
		return fragment;
	}

	public void addCardFragment(CardFragment fragment)
	{
		mFragments.add(fragment);
	}

	public void clean()
	{
		mFragments = new ArrayList<>();
	}
}
